/*
 * $Id: OpenAction.java 24 2009-03-31 23:40:10Z aaron.day $
 */
package org.crazydays.gameplan.data;


import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.crazydays.gameplan.GamePlan;
import org.crazydays.gameplan.swing.AbstractActionWrapper;

/**
 * OpenAction
 */
public class OpenAction
        extends AbstractActionWrapper
{
	/** serial version uid */
	public final static long serialVersionUID =
	        "$Id: OpenAction.java 24 2009-03-31 23:40:10Z aaron.day $"
	                .hashCode();

	/** game plan */
	protected GamePlan gamePlan;

	/**
	 * OpenAction constructor.
	 * 
	 * @param gamePlan GamePlan
	 */
	public OpenAction(GamePlan gamePlan)
	{
		super("Open Database");

		setGamePlan(gamePlan);
	}

	/**
	 * Set game plan.
	 * 
	 * @param gamePlan GamePlan
	 */
	public void setGamePlan(GamePlan gamePlan)
	{
		this.gamePlan = gamePlan;
	}

	/**
	 * Get game plan.
	 * 
	 * @return GamePlan
	 */
	public GamePlan getGamePlan()
	{
		return gamePlan;
	}

	/**
	 * @param event Event
	 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
	 */
	public void actionPerformed(ActionEvent event)
	{
		LOG.debug(event);

		// select and load a database
		JFileChooser chooser = new JFileChooser();
		FileNameExtensionFilter filter =
		        new FileNameExtensionFilter("GamePlan Database File",
		                "gp1");
		chooser.setFileFilter(filter);

		if (chooser.showOpenDialog(getGamePlan()) == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();

			LOG.info("Loading: " + file);

			// save
			try {
				getGamePlan().openDatabase(
				        DatabaseIO.load(file));
			} catch (IOException e) {
				LOG.error("Unable to save database", e);
				// TODO: present error
			}
		}
	}

}
